﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class BalanceCenter_AdminSendEmail : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!PublicFunction.CheckSystemRegisterState(this, "Email_ON"))
        {
            return;
        }

        if (!this.IsPostBack)
        {
            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        //RequestCompetences = Competences.BuildCompetencesList(Competences.SendMessage);//页面权限
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url

        base.OnInit(e);
    }

    #endregion

    private void BindData()
    {
        DataTable dt = new DAL.Views.V_Customer().Open("", "", "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        g.DataSource = dt;
        g.DataBind();
        gPager.Visible = (dt.Rows.Count > 0);
        Panel1.Visible = (dt.Rows.Count > 0);
        Panel2.Visible = false;
    }

    protected void g_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            //e.Item.Cells[10].Text = "";

            //int intCount = Shove._Convert.StrToInt(e.Item.Cells[12].Text, 0);  
            //for (int i = 0; i < intCount; i++)
            //{
            //    e.Item.Cells[10].Text += "<img src='../Images/gold.gif' border='0'>";
            //}
        }
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    protected void bt_AddEMail_ServerClick(object sender, EventArgs e)
    {
        System.Web.UI.WebControls.CheckBox chkBox;

        string SendUser = "";

        for (int i = 0; i < g.Items.Count; i++)
        {
            chkBox = (CheckBox)g.Items[i].FindControl("chkCompany");
            if (chkBox != null && chkBox.Checked)
            {
                if (SendUser != "")
                {
                    SendUser += ",";
                }

                SendUser += g.Items[i].Cells[6].Text;
            }
        }

        if (SendUser.ToString() != "")
        {
            tbSendEmailUser.Text = SendUser.ToString();

            Panel1.Visible = false;
            Panel2.Visible = true;
        }
    }

    protected void btnSendEmail_Click(object sender, EventArgs e)
    {
        if (tbTitle.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入邮件信息的标题！");

            return;
        }

        if (tbSendEmailContent.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入邮件信息的内容！");

            return;
        }

        if (tbSendEmailUser.Text == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请先添加联系人信息！");

            return;
        }

        //Send Email...
        string[] ToUserEmail = tbSendEmailUser.Text.Split(',');
        for (int i = 0; i < ToUserEmail.Length; i++)
        {
            PublicFunction.SendEmail(ToUserEmail[i], tbTitle.Text.Trim(), tbSendEmailContent.Text.Trim());
        }

        Panel1.Visible = true;
        Panel2.Visible = false;

        tbTitle.Text = "";
        tbSendEmailContent.Text = "";

        Shove._Web.JavaScript.Alert(this.Page, "邮件已发送");
        return;
    }

    protected void bt_AllUserEMail_ServerClick(object sender, EventArgs e)
    {
        string SendUser = "";
        DataTable dt = new DAL.Tables.tbl_Customer().Open("Email", "", "");

        try
        {
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["Email"].ToString() != "")
                {
                    if (SendUser != "")
                    {
                        SendUser += ",";
                    }

                    SendUser += dr["Email"].ToString();
                }
            }
        }
        catch
        {

        }
  
        if (SendUser.ToString() != "")
        {
            tbSendEmailUser.Text = SendUser.ToString();

            Panel1.Visible = false;
            Panel2.Visible = true;
        }
    }
}